<template>
    <a-card :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
            <h2 class="text-center  margin-bottom">{{description}}</h2>

            <a-form layout="inline" @keyup.enter.native="searchQuery">
                <a-row :gutter="24">
                    <a-col :md="8" :sm="24" v-if="showSelectArea">
                        <a-form-item label="区域查询">
                            <select-area ref="selectAreaRef" @change="changeArea"></select-area>
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="医疗机构">
                            <select-customer-list ref="selectCustomerRef" @change="changeCustomer"></select-customer-list>
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24" v-if="showCompany" >
                        <a-form-item label="处置单位">
                            <select-company-list :companysId="queryParam.companysId" @change="changeCompany"></select-company-list>
                        </a-form-item>
                    </a-col>

                </a-row>
            </a-form>
        </div>
        <!-- 查询区域-END -->

        <!-- 操作按钮区域 -->
        <div class="table-operator">
            <a-button type="primary" @click="searchQuery" icon="search">查询数据列表</a-button>
            <a-button type="primary" @click="resetForm">重置查询</a-button>
        </div>

        <!-- table区域-begin -->
        <div>
            <a-table
                    ref="table"
                    size="middle"
                    :scroll="{x:true}"
                    bordered
                    rowKey="id"
                    :columns="columns"
                    :dataSource="dataSource"
                    :pagination="ipagination"
                    :loading="loading"
                    class="j-table-force-nowrap"
                    @change="handleTableChange">

                <template slot="htmlSlot" slot-scope="text">
                    <div v-html="text"></div>
                </template>
                <template slot="imgSlot" slot-scope="text">
                    <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
                    <img v-else :src="getImgView(text)" height="25px" alt=""
                         style="max-width:80px;font-size: 12px;font-style: italic;"/>
                </template>
                <span slot="action" slot-scope="text, record">
                    <a  @click="handleDetails(record)">详情</a>
                </span>
            </a-table>
        </div>

        <customer-waste-list ref="customerWasteListRef" ></customer-waste-list>

    </a-card>
</template>

<script>

    import '@/assets/less/TableExpand.less'
    import { JeecgListMixin } from '@/mixins/JeecgListMixin'
    import SelectArea from '@/components/Jane/SelectArea'
    import SelectCompanyList from '@/components/Jane/SelectCompanyList'
    import SelectCustomerList from '@/components/Jane/SelectCustomerList'
    import CustomerWasteList from './modules/CustomerWasteList'

    export default {
        mixins: [JeecgListMixin],
        components: {
            SelectArea,
            SelectCustomerList,
            SelectCompanyList,
            CustomerWasteList,
        },
        data() {
            let that = this
            return {
                description: '待收运机构列表',
                // 表头
                columns: [
                    {
                        title: '序号',
                        dataIndex: '',
                        key: 'rowIndex',
                        width: 60,
                        align: 'center',
                        customRender: function(t, r, index) {
                            return parseInt(index) + 1
                        }
                    },
                    {
                        title: '机构名称',
                        align: 'center',
                        dataIndex: 'customerName'
                    },
                    {
                        title: '待收运废物量',
                        align: 'center',
                        dataIndex: 'wasteNum',
                        scopedSlots: { customRender: 'htmlSlot' },
                        customRender: function(value, row, index) {
                            return <div><span style="font-weight: bolder;" >{value || 0}</span> KG</div>
                        }
                    },
                    {
                        title: '机构地址',
                        align: 'center',
                        dataIndex: 'address',
                        customRender: function(t, r, index) {
                            return `${r.province || ''}${r.city || ''}${r.area || ''}`
                        }
                    },
                    {
                        title: '操作',
                        dataIndex: 'action',
                        align: 'center',
                        scopedSlots: { customRender: 'action' }
                    }
                ],
                url: {
                    /**
                     * 查询待收运机构
                     *  customerNumber （选填）{	customerDepartmentId }  部门ID
                     * 	companysId （选填）
                     *  recycleBagStatus  // 收运状态（1-已提交（待收运），2-已收运）
                     *  submitState  机构提交状态 0已提交  1已入暂存库  2已完成收运
                     *  areaCode
                     */
                    list: '/hc/hcRecycleBagcode/getWasteNumByArea',
                },
            }
        },
        created() {

        },
        computed: {
            showSelectArea: function() {
                let state = true;
                let userInfo = this.$store.getters.userInfo
                if (userInfo.provinceCode && userInfo.cityCode && userInfo.areaCode) {
                    state = false;
                }
                return state;
            },
            showCompany(){
                let state = false;
                let userInfo = this.$store.getters.userInfo
                if(!userInfo.companysId){
                    state = true;
                }
                return state;
            }
        },
        methods: {
            initDictConfig(){
                for (let i in this.queryParam) {
                    this.queryParam[i] = ''
                }
                // 获取到用户登陆的信息
                let userInfo = this.$store.getters.userInfo;
                if(userInfo.cityCode){
                    this.queryParam.cityCode = userInfo.cityCode;
                }
                if(userInfo.areaCode){
                    this.queryParam.areaCode = userInfo.areaCode;
                }
                // 待收运
                this.queryParam.recycleBagStatus = 1;

                if(userInfo.companysId){
                    this.queryParam.companysId = userInfo.companysId;
                }
                let ref = this.$refs.selectAreaRef;
                if(ref && ref.clearSelectArea != undefined){
                    ref.clearSelectArea();
                }
                let refCus = this.$refs.selectCustomerRef;
                if(refCus && refCus.clearSelectCustomer != undefined){
                    refCus.clearSelectCustomer();
                }

            },
            async changeArea({ area, city }) {
                if (city) {
                    this.queryParam.cityCode = city
                }
                if (area) {
                    this.queryParam.areaCode = area
                }
            },
            async changeCustomer(value,option) {
                this.queryParam.customerNumber = value;
                this.searchQuery()
            },
            async changeCompany(value, info,option) {
                this.queryParam.companysId = value;
                this.searchQuery()
            },
            searchQuery: function() {
                this.loadData(1)
            },
            handleDetails(row){
                let ref = this.$refs.customerWasteListRef;
                if(ref){
                    ref.show(row,this.queryParam.companysId);
                }
            },
            // 重置查询
            resetForm: function() {
                this.initDictConfig()
                this.searchQuery()
            },

        }
    }
</script>
<style scoped>
    @import '~@assets/less/common.less';
</style>
